
#region Usings

using PolyGameEngine.Tools;
using PolyGameEngine.MenuSystem;
using PolyGameEngine.GamePieces;
using System.IO;
using PolyMorph.LevelEditor;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion


namespace PolyMorph
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (Game1 game = new Game1())
            {
                game.Run();
            }
        }
    }
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : PhysicsGame
    {


        About AboutMenu;
        Help HelpMenu;
        LevelSelect LevelSelectMenu;
        MainMenu MainMenu;
        PauseMenu PauseMenu;

        public Game1()
        {
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();

            levelTest = new Level(this);
            AboutMenu = new About(Content, GraphicsDevice.Viewport, Fonts[0]);
            HelpMenu = new Help(Content, GraphicsDevice.Viewport, Fonts);
            LevelSelectMenu = new LevelSelect(Content, GraphicsDevice.Viewport, Fonts);
            MainMenu = new MainMenu(Content, GraphicsDevice.Viewport, Fonts);
            PauseMenu = new PauseMenu(Content, GraphicsDevice.Viewport, Fonts);

            MainMenu.PlayGameSelectedEvent += PlayMenuSelected;
            MainMenu.EditorSelectedEvent += EditorSelected;
            MainMenu.SelectLevelSelectedEvent += SelectLevelMenuSelected;

            LevelSelectMenu.LevelSelectedEvent += LevelSelected;

            AudioMan.Initialize(this);
            AudioMan.LoadSounds();
            AudioMan.PauseMusicOn();
            //AudioMan.menuPlay();
        }

        protected override void LoadContent()
        {
            Fonts[0] = Content.Load<SpriteFont>("font");
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void Update(GameTime gameTime)
        {
            if (gamestate == GameState.Play || gamestate == GameState.Edit)
                base.Update(gameTime);
            if (gamestate == GameState.Menu)
                MainMenu.update();
            if (gamestate == GameState.Pause)
                PauseMenu.update();
            if (gamestate == GameState.LevelSelect)
                LevelSelectMenu.update();
            if (gamestate == GameState.Help)
                HelpMenu.update();
            if (gamestate == GameState.About)
                AboutMenu.update();
            if (gamestate == GameState.Close)
            {
                this.Exit();
            }

            if (AudioMan.iLoop == 1)
                AudioMan.updateLoop();

            // TODO: Add your update logic here

        }

        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            if (gamestate == GameState.Play || gamestate == GameState.Edit)
            {
                if (!viewDebug)
                {
                    GraphicsDevice.Clear(Color.Black);
                    if (Background != null)
                        spriteBatch.Draw(Background, new Rectangle((int)GamePiece._ScreenOffset.X, (int)GamePiece._ScreenOffset.Y, (int)Level.levelSize.X, (int)Level.levelSize.Y), Color.White);
                }
                base.Draw(gameTime);
            }

            if (gamestate == GameState.Menu)
                MainMenu.Draw(spriteBatch);
            if (gamestate == GameState.Pause)
                PauseMenu.draw(spriteBatch);
            if (gamestate == GameState.LevelSelect)
                LevelSelectMenu.draw(spriteBatch);
            if (gamestate == GameState.Help)
                HelpMenu.draw(spriteBatch);
            if (gamestate == GameState.About)
                AboutMenu.draw(spriteBatch);

            spriteBatch.End();
        }

        Level levelTest;
        public void PlayMenuSelected()
        {
            string[] Levels = {"No Levels In Folder"};
            if (Directory.Exists(workingDir + @"\Levels\"))
                Levels = Directory.GetFileSystemEntries(workingDir + @"\Levels\");
            else
                Directory.CreateDirectory(workingDir + @"\Levels\");

            for (int i = 0; i < Levels.Length; i++)
            {
                int start = Levels[i].IndexOf(@"\Levels\");
                Levels[i] = Levels[i].Substring(start + 8);
            }

            if (Levels.Length == 0)
            {
                Levels = new string[1];
                Levels[0] = "No Levels In Folder";
            }
            LevelSelectMenu.levels = Levels;
            if (!(Levels == null || Levels.Length == 0))
                levelTest.PlayMode();
            else
                gamestate = GameState.Menu;
        }
        public void EditorSelected()
        {
            levelTest.EditMode();
        }

        public void LevelSelected(string Name)
        {
            levelTest.PlayMode(Name);
        }


        static string workingDir = Directory.GetCurrentDirectory();
        public void SelectLevelMenuSelected()
        {

            string[] Levels = Directory.GetFileSystemEntries(workingDir+@"\Levels\");
            
            for (int i = 0; i < Levels.Length; i++)
            {
                int start =  Levels[i].IndexOf(@"\Levels\");
                Levels[i] = Levels[i].Substring(start + 8);
            }

            if (Levels.Length == 0)
            {
                Levels = new string[1];
                Levels[0] = "No Levels In Folder";
            }
            LevelSelectMenu.levels = Levels;
        }

        
    }
}
